<?php

namespace app\admin\controller;

use think\Request;
use app\admin\model\Cate as CateModel;
use think\Db;
class Cate extends Base
{

    private $cate;

    function initialize()
    {
        parent::initialize();
        $this->cate = new CateModel();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $where = null;
        $param = input('get.');
        if( !empty($param['search']) ){
            $where=$param['search'];
        }else{
            $where='%%';
        }
        $cates=$this->cate
            ->name('infocate')
            ->where('catename','like','%'.$where.'%')
            ->select();
        $this->assign('cates', $cates);
        return view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $info = $request->post();
        // 处理数据
        $data = [
            'catename' => $info['catename'],
            'ckeywords' => $info['ckeywords'],
            'desc' => $info['cate_desc'],
        ];
        $validate = new \app\admin\validate\cate;
        if($validate->check($data)){
        $result = $this->cate->name('infocate')->data($data)->insert();
        if($result){
            //跳转地址要写成路由形式
            return  $this->redirect('/cate');
        }else{
            $this->error('新增栏目失败!');
            }
        }else{
            return $this->error($validate->getError());
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $cates = $this->cate->name('infocate')->find($id);
        return json($cates);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        // 是否是AJAX请求
        if (!request()->isAjax()) {
            $this->error('你四不四迷路了!', '/');
        }
        $result=$this->cate->name('infocate')->find($id);
        return json($result);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $info = $request->put();
        // 处理数据
        $data = [
            'catename' => $info['catename'],
            'ckeywords' => $info['ckeywords'],
            'desc' => $info['cate_desc'],
        ];
        $validate = new \app\admin\validate\cate;
        if($validate->scene('edit')->check($data)){
            $cate_up = $this->cate->name('infocate')->where('id', $id)->update($data);
        if($cate_up){
            //跳转地址要写成路由形式
            return  $this->redirect('/cate');
        }else{
            $this->error('更新资讯失败!');
             }
        }else{
            return $this->error($validate->getError());
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $cateresult = $this->cate->name('informations')->where('cateid',$id)->delete();
        $cateresult = $this->cate->name('infocate')->delete($id);
        if ($cateresult > 0) {
            $info['status'] = true;
            $info['info'] = 'ID为: ' . $id . '的栏目,删除成功!';
        } else {
            $info['status'] = false;
            $info['info'] = 'ID为: ' . $id . '的栏目,删除失败,请重试....';
        }

        return json($info);
    }
}
